<template>
	<view class="pay-type-box" :style="{ '--color': vuex_config.themeColor1 }">
		<view class="pay-type-title u-border-bottom">选择支付方式</view>
		<view class="pay-type-cell" @click.stop="$onClick(item.value)" v-for="(item,index) in list" :key="item.value">
			<u-icon :name="item.icon" size="44" :color="item.iconColor" custom-prefix="iconfont"></u-icon>
			<view class="ptc-name">{{item.name}}</view>
			<view class="ptc-radio" :class="{'ptc-radio-act': value == item.value}">
				<u-icon :name="value == item.value ? 'e730' : 'e72f'" size="34" custom-prefix="iconfont"></u-icon>
			</view>
		</view>

		<view class="pay-type-cell" @click.stop="$onClick('2')" v-if="isBalance">
			<u-icon name="e653" size="44" color="#F38200" custom-prefix="iconfont"></u-icon>
			<view class="ptc-name">余额支付（{{ $price(vuex_count.balance) }}）</view>
			<view class="ptc-radio" :class="{ 'ptc-radio-act': value === '2' }">
				<u-icon :class="[!vuex_count.balance ? 'icon_disabled' : '']" :name="value === '2' ? 'e730' : 'e72f'"
					size="34" custom-prefix="iconfont"></u-icon>
			</view>
		</view>
		<slot></slot>
	</view>
</template>
<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	import {
		balance
	} from '@/mixins/balance.js'
	export default {
		mixins: [balance],
		props: {
			value: {
				type: [String, Number],
				default: '1',
			}, // 支付类型：1 微信（百度）支付 2:余额支付 6:环迅支付  
			price: {
				type: [String, Number],
				default: 0
				// validator: (val) => {
				//   return val ? parseFloat(val) : 0
				// }
			},
			isBalance:{
				type:Boolean,
				default:true
			},// 是否有余额支付方式
		},
		data () {
			return {
				list:[]
			}
		},
		computed: {
			...mapState(['vuex_count'])
		},
		mounted() {
			let patyType = 1
			// #ifdef MP-WEIXIN || H5 
			patyType = this.vuex_config.union_pay == 1?6:1
			// patyType = this.vuex_config.union_pay
			this.list = [
				{
					value:patyType,
					icon:'e606',
					iconColor:'#18b566',
					name:'微信支付',
				}
			]
			// #endif
			// #ifdef APP-PLUS
			patyType = this.vuex_config.union_pay == 1?6:1
			// patyType = this.vuex_config.union_pay
			this.list = [
				{
					value:patyType,
					icon:'e606',
					iconColor:'#18b566',
					name:'微信支付',
				}
			]
			if(this.vuex_businessObj.alipayappid){
				this.list = this.list.concat([{
					value:7,
					icon:'e60b',
					iconColor:'#5199FF',
					name:'支付宝',
				}])
			}
			// #endif
			// #ifdef MP-ALIPAY
			patyType = 7
			this.list = [
				{
					value:7,
					icon:'e60b',
					iconColor:'#5199FF',
					name:'支付宝',
				}
			]
			// #endif
			// #ifdef MP-BAIDU
			patyType = 8
			this.list = [
				{
					value:8,
					icon:'e8cb',
					iconColor:'#5199FF',
					name:'百度',
				}
			]
			// #endif
			// #ifdef MP-TOUTIAO	
			patyType = 9
			this.list = [
				{
					value:9,
					icon:'e638',
					iconColor:'#5199FF',
					name:'字节',
				}
			]
			// #endif
			this.$emit('input',patyType)
			// 获取用户余额
			this.loadUserCount();
		},
		methods: {
			...mapActions(['loadUserCount']), // 这个应该放到组件查询
			$onClick(type) {
				this.$u.debounce(
					() => {
						if (type === '2') {
							if (!this.vuex_count.balance || parseFloat(this.vuex_count.balance) < this.price) {
								uni.showModal({
									title: '余额不足',
									content: '是否立即充值?',
									success: res => {
										if (res.confirm) {
											let amount = this.price - parseFloat(this.vuex_count.balance ||
												0);
											this.$loading();
											this.Addrecharge(amount)
												.then(() => {
													this.loadUserCount();
													this.$hide();
												})
												.catch(() => {
													this.$hide();
												});
										}
									}
								});
								return;
							}
						}
						this.$emit('input', type);
					},
					300,
					true
				);
			}
		}
	};
</script>
<style lang="scss" scoped>
	.pay-type-box {
		width: 100%;

		.pay-type-title {
			width: 100%;
			height: 90rpx;
			display: flex;
			align-items: center;
			font-size: 32rpx;
			font-weight: bold;
		}

		.pay-type-cell {
			width: 100%;
			height: 104rpx;
			display: flex;
			align-items: center;

			.ptc-name {
				flex: 1;
				margin-left: 20rpx;
				font-size: 28rpx;
				color: #666666;
			}

			.ptc-radio {
				color: #999999;

				&.ptc-radio-act {
					color: var(--color);
				}

				.icon_disabled {
					color: #ddd;
				}
			}
		}
	}
</style>
